
namespace KlearTweet.Client.Panels
{
    public class NewTweetController
    {
        private readonly Initialization.AccountsManager _accountsManager;
        private readonly NewTweetView _view;
        public NewTweetController(System.Windows.Controls.Primitives.ToggleButton toggleButton, NewTweetView view,
            Initialization.AccountsManager accountsManager)
        {
            _view = view;
            _view.Height = 0; // Hidden by default // TODO: Change height because, when using Collapse, Text.Focus() doesn't work the first time
            _accountsManager = accountsManager;

            toggleButton.Checked += (s, e) =>
            {
                _view.Height = double.NaN; // Auto
                _view.Text.Focus();
            };
            toggleButton.Unchecked += (s, e) => _view.Height = 0;

            _view.Send.Click += (s, e) => SendTweet();
        }

        private void SendTweet()
        {
            var accounts = _accountsManager.GetAccounts(); // TODO: Should get the selected account!
            if (accounts.Count == 0)
            {
                DataContainers.InfoController.Show("An account is required!");
                return;
            }
            WebServices.TwitterClient.SendTweet(accounts[0], _view.Text.QueryText().Text,
                response => DataContainers.InfoController.Show("Tweet sent: " + response)); // TODO: Don't show response + If success, clear _view.Text
        }
    }
}